# -*- coding = utf-8 -*-

import socket

# 源ip地址与端口
src_ip = "fe80::4c6a:f242:86d2:1511"
src_port = 0    # 0 表示任意端口

# 目的ip地址与端口
dest_ip = "fe80::4c6a:f242:86d2:1511"
dest_port = 10000

# 创建套接字
sock = socket.socket(socket.AF_INET6, socket.SOCK_DGRAM, socket.IPPROTO_UDP)
sock.bind((src_ip, src_port))

# 获取自动分配的源端口
src_port_assigned = sock.getsockname()[1]

# 需要发送的数据
# 字符串
# data = b"hello this is udp packet"

hex_data = "00 11 22 33 44 55 66 77 88 99 AA BB CC DD EE FF"

data = bytes.fromhex(hex_data)

# 发送数据
sock.sendto(data, (dest_ip, dest_port))
print(f"src  ip = {src_ip} port = {hex(src_port_assigned)}")
print(f"dest ip = {dest_ip} port = {hex(dest_port)}")
print(f"data = {' '.join(format(x, '02x') for x in data)}")

# 关闭套接字
sock.close()
